
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java Future · tgor note</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tgor">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Java之Future.html" />
    
    
    <link rel="prev" href="Java之Lock.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../category/微服务.html">
            
                <a href="../category/微服务.html">
            
                    
                    微服务
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Java-Spring/Feign.html">
            
                <a href="../Java-Spring/Feign.html">
            
                    
                    HTTP客户端Feign
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Java-其他库/限流.html">
            
                <a href="../Java-其他库/限流.html">
            
                    
                    限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    限流Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../Java-Spring/负载均衡.html">
            
                <a href="../Java-Spring/负载均衡.html">
            
                    
                    负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    负载均衡Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../Java-其他库/熔断Hystrix.html">
            
                <a href="../Java-其他库/熔断Hystrix.html">
            
                    
                    熔断Hystrix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html#本地缓存">
            
                    
                    本地缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    分布式缓存Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html">
            
                    
                    分布式事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html#事务消息">
            
                    
                    可靠事务消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../Java-Spring/业务监控.html">
            
                <a href="../Java-Spring/业务监控.html">
            
                    
                    业务监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../Java-其他库/分布式定时作业.html">
            
                <a href="../Java-其他库/分布式定时作业.html">
            
                    
                    分布式作业调度ElasticJob
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../Java-ORM/ORM.html">
            
                <a href="../Java-ORM/ORM.html">
            
                    
                    ORM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../Java-其他库/分布式系统及CAP原理.html">
            
                <a href="../Java-其他库/分布式系统及CAP原理.html">
            
                    
                    分布式系统及CAP原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../MQ/RabbitMQ笔记.html">
            
                <a href="../MQ/RabbitMQ笔记.html">
            
                    
                    消息驱动Rabbitmq
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../category/数据库.html">
            
                <a href="../category/数据库.html">
            
                    
                    数据库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../Mysql/经典数据库设计案例.html">
            
                <a href="../Mysql/经典数据库设计案例.html">
            
                    
                    mysql设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../Mysql/Mysql事务与锁.html">
            
                <a href="../Mysql/Mysql事务与锁.html">
            
                    
                    mysql事务与锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Mysql/mysql数据类型.html">
            
                <a href="../Mysql/mysql数据类型.html">
            
                    
                    mysql数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html">
            
                    
                    mysql索引与查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html#查询语句设计准则">
            
                    
                    mysql查询语句设计准则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../Mysql/mysql性能测试.html">
            
                <a href="../Mysql/mysql性能测试.html">
            
                    
                    mysql性能测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../Mysql/mysql主从同步与读写分离.html">
            
                <a href="../Mysql/mysql主从同步与读写分离.html">
            
                    
                    mysql主从同步与读写分离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                <a href="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                    
                    Spring事务注解@Transactional
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../数据结构与算法/位运算应用实例.html">
            
                <a href="../数据结构与算法/位运算应用实例.html">
            
                    
                    位运算应用实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../Mongodb-Client/morphia-API汇总.html">
            
                <a href="../Mongodb-Client/morphia-API汇总.html">
            
                    
                    morphia API汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../Mongodb-Client/mongo驱动连接.html">
            
                <a href="../Mongodb-Client/mongo驱动连接.html">
            
                    
                    mongodb驱动与连接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../Mongodb/mongodb-ObjectId实践.html">
            
                <a href="../Mongodb/mongodb-ObjectId实践.html">
            
                    
                    mongodb ObjectId实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../Mongodb/mongodb角色权限.html">
            
                <a href="../Mongodb/mongodb角色权限.html">
            
                    
                    mongodb角色权限
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../category/java并发.html">
            
                <a href="../category/java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Java并发.html">
            
                <a href="Java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Java并发.html">
            
                <a href="Java并发.html#锁">
            
                    
                    Java并发之锁设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Java并发.html">
            
                <a href="Java并发.html#synchronized">
            
                    
                    Java并发之synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantlock">
            
                    
                    Java Lock之ReentrantLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantreadwritelock">
            
                    
                    Java Lock之ReentrantReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.6" data-path="Java之Future.html">
            
                <a href="Java之Future.html">
            
                    
                    Java Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Java之Future.html">
            
                <a href="Java之Future.html#futuretask">
            
                    
                    Java Future之FutureTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="Java之Future.html">
            
                <a href="Java之Future.html#completablefuture">
            
                    
                    Java Future之CompletableFuture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="Java之Future.html">
            
                <a href="Java之Future.html#forkjointask">
            
                    
                    Java Future之ForkJoinTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="Java之Thread.html">
            
                <a href="Java之Thread.html">
            
                    
                    Java Thread
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../category/redis.html">
            
                <a href="../category/redis.html">
            
                    
                    Redis
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#集群">
            
                    
                    Redis集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#消息订阅">
            
                    
                    Redis消息订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#lua">
            
                    
                    Redis Lua脚本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#配置汇总">
            
                    
                    Redis配置汇总
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../category/运维.html">
            
                <a href="../category/运维.html">
            
                    
                    运维
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../运维/Linux常用操作汇总.html">
            
                <a href="../运维/Linux常用操作汇总.html">
            
                    
                    Linux常用操作汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../运维/阿里云笔记.html">
            
                <a href="../运维/阿里云笔记.html">
            
                    
                    阿里云笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../category/nginx.html">
            
                <a href="../category/nginx.html">
            
                    
                    Nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html">
            
                    
                    Nginx基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    Nginx负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    Nginx限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html#六、其他">
            
                    
                    Nginx客户端辅助技巧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../category/容器化.html">
            
                <a href="../category/容器化.html">
            
                    
                    容器化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html">
            
                    
                    Docker学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#基础操作">
            
                    
                    Docker基础操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#dockerfile">
            
                    
                    DockerFile
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../category/Web.html">
            
                <a href="../category/Web.html">
            
                    
                    Web
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../运维/Http入坑Https笔记.html">
            
                <a href="../运维/Http入坑Https笔记.html">
            
                    
                    Http入坑Https笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../Web/阿里云OSS.html">
            
                <a href="../Web/阿里云OSS.html">
            
                    
                    OSS
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../category/java基础.html">
            
                <a href="../category/java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="Java基础.html">
            
                <a href="Java基础.html#jvm">
            
                    
                    JVM基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="Java基础.html">
            
                <a href="Java基础.html#语法糖">
            
                    
                    Java语法糖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="Java基础.html">
            
                <a href="Java基础.html#数据结构">
            
                    
                    Java数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="Java8函数式编程.html">
            
                <a href="Java8函数式编程.html">
            
                    
                    java8函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="ArrayList源码分析.html">
            
                <a href="ArrayList源码分析.html">
            
                    
                    JDK源码分析ArrayList
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="HashMap源码分析.html">
            
                <a href="HashMap源码分析.html">
            
                    
                    JDK源码分析HashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="Java注解.html">
            
                <a href="Java注解.html">
            
                    
                    Java注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="Timsort排序算法.html">
            
                <a href="Timsort排序算法.html">
            
                    
                    Timsort排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="插入排序分析与Java实现.html">
            
                <a href="插入排序分析与Java实现.html">
            
                    
                    插入排序分析与Java实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../category/工具与规范.html">
            
                <a href="../category/工具与规范.html">
            
                    
                    工具与规范
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../工具/Maven使用技巧汇总.html">
            
                <a href="../工具/Maven使用技巧汇总.html">
            
                    
                    Maven使用技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../编码规范/Java编码规范.html">
            
                <a href="../编码规范/Java编码规范.html">
            
                    
                    Java编码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../category/OLAP.html">
            
                <a href="../category/OLAP.html">
            
                    
                    OLAP
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../数据处理/storm学习笔记.html">
            
                <a href="../数据处理/storm学习笔记.html">
            
                    
                    流式计算Storm学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../category/数据运营.html">
            
                <a href="../category/数据运营.html">
            
                    
                    数据运营
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../数据运营/增长黑客数据运营.html">
            
                <a href="../数据运营/增长黑客数据运营.html">
            
                    
                    增长黑客-数据运营
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../category/前端技术栈.html">
            
                <a href="../category/前端技术栈.html">
            
                    
                    前端技术栈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../前端/JavaScript设计模式.html">
            
                <a href="../前端/JavaScript设计模式.html">
            
                    
                    JavaScript设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../前端/html相对路径与绝对路径.html">
            
                <a href="../前端/html相对路径与绝对路径.html">
            
                    
                    Html相对路径与绝对路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../前端/微信小程序学习笔记.html">
            
                <a href="../前端/微信小程序学习笔记.html">
            
                    
                    微信小程序学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../category/个人.html">
            
                <a href="../category/个人.html">
            
                    
                    个人
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../个人/简历.html">
            
                <a href="../个人/简历.html">
            
                    
                    简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Java Future</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="java-future">Java Future</h1>
<p>&#x5E76;&#x53D1;&#x7F16;&#x7A0B;&#x7684;&#x672C;&#x8D28;&#x5C31;&#x662F;&#xFF0C;&#x591A;&#x6761;&#x7EBF;&#x7A0B;&#x5E76;&#x884C;&#x8FD0;&#x884C;&#xFF0C;&#x63D0;&#x5347;CPU&#x5229;&#x7528;&#x7387;&#xFF0C;&#x63D0;&#x5347;&#x5E76;&#x53D1;&#x80FD;&#x529B;</p>
<p>&#x800C;Future&#x63A5;&#x53E3;&#x8981;&#x89E3;&#x51B3;&#x7684;&#x95EE;&#x9898;&#x5C31;&#x662F;&#xFF0C;&#x7AD9;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x7684;&#x7ACB;&#x573A;&#xFF0C;&#x66F4;&#x597D;&#x5730;&#x64CD;&#x4F5C;&#x3001;&#x7BA1;&#x7406;<strong>&#x8F85;&#x52A9;&#x7EBF;&#x7A0B;</strong>&#x5904;&#x7406;&#x5E76;&#x884C;&#x4EFB;&#x52A1;</p>
<h1 id="runnable&#x3001;callable&#x4E0E;future">Runnable&#x3001;Callable&#x4E0E;Future</h1>
<p>3&#x4E2A;<strong>&#x63A5;&#x53E3;</strong>&#x533A;&#x522B;</p>
<ul>
<li><code>Runnable</code>&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x4EFB;&#x52A1;&#x4E4B;&#x540E;&#x65E0;&#x6CD5;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x7ED3;&#x679C;</li>
<li><code>Callable</code>&#xFF0C;&#x4F4D;&#x4E8E;java.util.concurrent&#x5305;&#x4E0B;&#xFF0C;&#x589E;&#x52A0;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;</li>
<li><code>Future</code>&#xFF0C;&#x662F;&#x5BF9;Runnable&#x4E0E;Callable&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#x4E0B;&#x7684;&#x8865;&#x5145;</li>
</ul>
<pre><code class="lang-java"><span class="hljs-comment">//&#x7531;&#x4E8E;run()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;void&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x6267;&#x884C;&#x5B8C;&#x4EFB;&#x52A1;&#x4E4B;&#x540E;&#x65E0;&#x6CD5;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x7ED3;&#x679C;&#x3002;</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Runnable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-comment">//Callable&#x4F4D;&#x4E8E;java.util.concurrent&#x5305;&#x4E0B;&#xFF0C;&#x6CDB;&#x578B;V&#x4E3A;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Callable</span>&lt;<span class="hljs-title">V</span>&gt; </span>{
    <span class="hljs-function">V <span class="hljs-title">call</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception</span>;
}

<span class="hljs-comment">//Future&#x63A5;&#x53E3;&#x5728;Runnable&#x4E0E;Callable&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#x4E0B;&#x7684;&#x8865;&#x5145;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9;&#x4EE5;&#x4E0A;&#x4E24;&#x4E2A;&#x63A5;&#x53E3;&#x4EFB;&#x52A1;&#x7684;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x83B7;&#x53D6;&#x3001;&#x53D6;&#x6D88;&#x3001;&#x9A8C;&#x8BC1;&#x5B8C;&#x6210;&#x72B6;&#x6001;&#x7B49;&#x64CD;&#x4F5C;</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Future</span>&lt;<span class="hljs-title">V</span>&gt; </span>{
    <span class="hljs-comment">//&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x53D6;&#x6D88;&#x4EFB;&#x52A1;&#xFF0C;&#x5982;&#x679C;&#x53D6;&#x6D88;&#x4EFB;&#x52A1;&#x6210;&#x529F;&#x5219;&#x8FD4;&#x56DE;true&#xFF0C;&#x5982;&#x679C;&#x53D6;&#x6D88;&#x4EFB;&#x52A1;&#x5931;&#x8D25;&#x5219;&#x8FD4;&#x56DE;false</span>
    <span class="hljs-comment">//&#x53C2;&#x6570;mayInterruptIfRunning&#x8868;&#x793A;&#x662F;&#x5426;&#x5141;&#x8BB8;&#x53D6;&#x6D88;&#x6B63;&#x5728;&#x6267;&#x884C;&#x5374;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x7684;&#x4EFB;&#x52A1;</span>
    <span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">cancel</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> mayInterruptIfRunning)</span></span>;

    <span class="hljs-comment">//&#x8868;&#x793A;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x88AB;&#x53D6;&#x6D88;&#x6210;&#x529F;</span>
    <span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isCancelled</span><span class="hljs-params">()</span></span>;

    <span class="hljs-comment">//&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;</span>
    <span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isDone</span><span class="hljs-params">()</span></span>;

    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x4EA7;&#x751F;&#x963B;&#x585E;</span>
    <span class="hljs-function">V <span class="hljs-title">get</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException</span>;

    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x8FD8;&#x6CA1;&#x83B7;&#x53D6;&#x5230;&#x7ED3;&#x679C;&#xFF0C;&#x5C31;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;null</span>
    <span class="hljs-function">V <span class="hljs-title">get</span><span class="hljs-params">(<span class="hljs-keyword">long</span> timeout, TimeUnit unit)</span>
        <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException, TimeoutException</span>;
}
</code></pre>
<p>Future&#x63A5;&#x53E3;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;</p>
<p><img src="assets/1580466059523.png" alt="1580466059523"></p>
<p>&#x5E38;&#x7528;&#x4E3B;&#x8981;&#x6709;</p>
<ul>
<li><a href="#futuretask">FutureTask</a></li>
<li><a href="#competablefuture">CompetableFuture</a></li>
<li><a href="#forkjointask">ForkJoinTask</a></li>
</ul>
<h1 id="futuretask">FutureTask</h1>
<p>&#x5F15;&#x7528;&#x5173;&#x7CFB;&#x56FE;</p>
<p><img src="assets/1580466305535.png" alt="1580466305535"></p>
<p>&#x4F7F;&#x7528;&#x793A;&#x4F8B;&#xFF1A;&#x5206;&#x522B;&#x4EE5;<code>Runnable</code>&#x548C;<code>Callable</code>&#x65B9;&#x5F0F;&#x6267;&#x884C;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">runnableExample</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>{
    String result = <span class="hljs-string">&quot;&quot;</span>;
    <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> FutureTask&lt;String&gt;(() -&gt; {
        System.out.println(<span class="hljs-string">&quot;&#x6B63;&#x5728;&#x6267;&#x884C;FutureTask&quot;</span>);
        threadSleep(<span class="hljs-number">100</span>); 
    }, result)).start();
}

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">callableExample</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>{
    FutureTask&lt;String&gt; futureTask1 = <span class="hljs-keyword">new</span> FutureTask&lt;String&gt;(() -&gt; {
        threadSleep(<span class="hljs-number">200</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;1&quot;</span>;
    });

    FutureTask&lt;String&gt; futureTask2 = <span class="hljs-keyword">new</span> FutureTask&lt;String&gt;(() -&gt; {
        threadSleep(<span class="hljs-number">100</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;2&quot;</span>;
    });

    FutureTask&lt;String&gt; futureTask3 = <span class="hljs-keyword">new</span> FutureTask&lt;String&gt;(() -&gt; {
        threadSleep(<span class="hljs-number">600</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;3&quot;</span>;
    });

    ExecutorService executor = Executors.newFixedThreadPool(<span class="hljs-number">3</span>);
    executor.execute(futureTask1);
    executor.execute(futureTask2);
    executor.execute(futureTask3);

    <span class="hljs-keyword">if</span> (futureTask1.isDone() &amp;&amp; futureTask2.isDone() &amp;&amp; futureTask3.isDone()) {
        System.out.println(<span class="hljs-string">&quot;Done&quot;</span>);
        executor.shutdown();
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">if</span> (!futureTask1.isDone()) {
        <span class="hljs-comment">//get()&#x963B;&#x585E;&#x7B49;&#x5F85;&#x7ED3;&#x679C;</span>
        System.out.println(<span class="hljs-string">&quot;Task1&#x8F93;&#x51FA;&#x7ED3;&#x679C;&quot;</span> + futureTask1.get());
    }

    <span class="hljs-keyword">if</span> (futureTask2.isDone()) {
        <span class="hljs-comment">//task2 100ms&#x5B8C;&#x6210;&#xFF0C;&#x5148;&#x4E8E;task1&#x5B8C;&#x6210;&#xFF0C;get()&#x65E0;&#x963B;&#x585E;</span>
        System.out.println(<span class="hljs-string">&quot;Task2&#x8F93;&#x51FA;&#x7ED3;&#x679C;&quot;</span> + futureTask2.get());
    } <span class="hljs-keyword">else</span> {
        System.out.println(<span class="hljs-string">&quot;&#x7B49;&#x5F85;Task2&#x5B8C;&#x6210;&quot;</span>);
    }

    <span class="hljs-keyword">try</span> {
        <span class="hljs-comment">//task1&#x8DD1;&#x4E86;200ms&#xFF0C;task3&#x8981;&#x8DD1;600ms&#xFF0C;&#x8BBE;100ms&#x8D85;&#x65F6;t3&#x4ECD;&#x7136;&#x672A;&#x80FD;&#x5B8C;&#x6210;</span>
        String s = futureTask3.get(<span class="hljs-number">100L</span>, TimeUnit.MILLISECONDS);
        <span class="hljs-keyword">if</span> (s != <span class="hljs-keyword">null</span>) {
            System.out.println(<span class="hljs-string">&quot;Task3&#x8F93;&#x51FA;&#x7ED3;&#x679C;&quot;</span> + s);
        }
    } <span class="hljs-keyword">catch</span> (TimeoutException ex) {
        System.out.println(<span class="hljs-string">&quot;Task3&#x8D85;&#x65F6;&quot;</span>);
    }

    executor.shutdown();
}
<span class="hljs-comment">//&#x8F93;&#x51FA;&#x7ED3;&#x679C;</span>
Task1&#x8F93;&#x51FA;&#x7ED3;&#x679C;<span class="hljs-number">1</span>
Task2&#x8F93;&#x51FA;&#x7ED3;&#x679C;<span class="hljs-number">2</span>
Task3&#x8D85;&#x65F6;
</code></pre>
<h2 id="&#x72B6;&#x6001;">&#x72B6;&#x6001;</h2>
<table>
<thead>
<tr>
<th>&#x679A;&#x4E3E;</th>
<th>&#x72B6;&#x6001;</th>
<th>&#x4EE3;&#x7801;</th>
</tr>
</thead>
<tbody>
<tr>
<td>NEW</td>
<td>&#x65B0;&#x5EFA;</td>
<td>0</td>
</tr>
<tr>
<td>COMPLETING</td>
<td>&#x6B63;&#x5728;&#x5B8C;&#x6210;</td>
<td>1</td>
</tr>
<tr>
<td>NORMAL</td>
<td>&#x6B63;&#x5E38;</td>
<td>2</td>
</tr>
<tr>
<td>EXCEPTIONAL</td>
<td>&#x7279;&#x6B8A;</td>
<td>3</td>
</tr>
<tr>
<td>CANCELLED</td>
<td>&#x53D6;&#x6D88;</td>
<td>4</td>
</tr>
<tr>
<td>INTERRUPTING</td>
<td>&#x4E2D;&#x65AD;&#x4E2D;</td>
<td>5</td>
</tr>
<tr>
<td>INTERRUPTED</td>
<td>&#x5DF2;&#x7EC8;&#x7AEF;</td>
<td>6</td>
</tr>
</tbody>
</table>
<p>&#x5168;&#x90E8;&#x72B6;&#x6001;&#x8F6C;&#x6362;&#x60C5;&#x51B5;</p>
<ul>
<li>NEW -&gt; COMPLETING -&gt; NORMAL</li>
<li>NEW -&gt; COMPLETING -&gt; EXCEPTIONAL</li>
<li>NEW -&gt; CANCELLED</li>
<li>NEW -&gt; INTERRUPTING -&gt; INTERRUPTED</li>
</ul>
<h2 id="&#x56FE;&#x89E3;">&#x56FE;&#x89E3;</h2>
<p><img src="assets/FutureTask1.png" alt="1580466305536"></p>
<p>&#x6CE8;&#xFF1A;&#x7EFF;&#x8272;&#x8868;&#x793A;FutureTask&#x5185;&#x90E8;&#x53D8;&#x91CF;</p>
<p>FutureTask&#x5185;&#x90E8;&#x6838;&#x5FC3;&#x53D8;&#x91CF;</p>
<ul>
<li>runner&#xFF1A;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;</li>
<li>state&#xFF1A;&#x5F53;&#x524D;&#x4EFB;&#x52A1;&#x72B6;&#x6001;</li>
<li>waiter&#xFF1A;&#x7B49;&#x5F85;&#x4EFB;&#x52A1;&#x7ED3;&#x679C;&#x7684;&#x7EBF;&#x7A0B;</li>
</ul>
<p>&#x5173;&#x952E;&#x6D41;&#x7A0B;</p>
<ul>
<li>&#x901A;&#x8FC7;cas&#x8BBE;&#x7F6E;runner&#x51B3;&#x5B9A;&#x4EFB;&#x52A1;task&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;</li>
<li></li>
</ul>
<p>&#x56FE;&#x89E3;&#x793A;&#x4F8B;&#x4EE3;&#x7801;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{
    <span class="hljs-keyword">final</span> FutureTask&lt;String&gt; futureTask1 = <span class="hljs-keyword">new</span> FutureTask&lt;String&gt;(<span class="hljs-keyword">new</span> GetBlock(<span class="hljs-number">9</span>)) {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">done</span><span class="hljs-params">()</span> </span>{
            System.out.println(<span class="hljs-string">&quot;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;x&quot;</span>);
            <span class="hljs-keyword">super</span>.done();
        }
    };

    ExecutorService executor = Executors.newFixedThreadPool(<span class="hljs-number">4</span>);
    <span class="hljs-comment">//&#x542F;&#x52A8;2&#x6761;&#x6267;&#x884C;task&#x7684;&#x7EBF;&#x7A0B;</span>
    executor.submit(futureTask1);<span class="hljs-comment">//A</span>
    executor.submit(futureTask1);<span class="hljs-comment">//B</span>

    TimeUnit.MILLISECONDS.sleep(<span class="hljs-number">200</span>);

    <span class="hljs-comment">//&#x542F;&#x52A8;2&#x6761;&#x7B49;&#x5F85;task&#x7ED3;&#x679C;&#x7684;&#x7EBF;&#x7A0B;</span>
    executor.execute(() -&gt; {
        <span class="hljs-keyword">try</span> {
            String s = futureTask1.get();
            System.out.println(<span class="hljs-string">&quot;&#x7ED3;&#x679C;1&#xFF1A;&quot;</span> + s);
        } <span class="hljs-keyword">catch</span> (ExecutionException | InterruptedException ex) {
            ex.printStackTrace();
        }
    });
    executor.execute(() -&gt; {
        <span class="hljs-keyword">try</span> {
            String s = futureTask1.get();
            System.out.println(<span class="hljs-string">&quot;&#x7ED3;&#x679C;2&#xFF1A;&quot;</span> + s);
        } <span class="hljs-keyword">catch</span> (ExecutionException | InterruptedException ex) {
            ex.printStackTrace();
        }
    });

    executor.shutdown();
}

<span class="hljs-comment">//&#x8F93;&#x51FA;&#x7ED3;&#x679C;</span>
&#x6B21;&#x6570;<span class="hljs-number">0</span>
&#x4EFB;&#x52A1;&#x5B8C;&#x6210;x
&#x7ED3;&#x679C;<span class="hljs-number">1</span>&#xFF1A;<span class="hljs-number">9</span>
&#x7ED3;&#x679C;<span class="hljs-number">2</span>&#xFF1A;<span class="hljs-number">9</span>
</code></pre>
<h2 id="&#x6E90;&#x7801;">&#x6E90;&#x7801;</h2>
<p>&#x6267;&#x884C;&#x64CD;&#x4F5C;<code>run()</code></p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">if</span> (cas&#x8BBE;&#x7F6E;runner&#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5931;&#x8D25;)
        <span class="hljs-keyword">return</span>;
    <span class="hljs-keyword">try</span> {
        Callable&lt;V&gt; c = callable;
        result = c.call();
        <span class="hljs-keyword">if</span>(exception){
            setException(ex);
        }          
        <span class="hljs-keyword">else</span>{
            set(result);
        }

    } <span class="hljs-keyword">finally</span> {
        <span class="hljs-keyword">if</span> (state&#x4E3A;&#x4E2D;&#x65AD;&#x4E2D;&#xFF0C;&#x6216;&#x5DF2;&#x7EC8;&#x7AEF;)
            handlePossibleCancellationInterrupt(s);
    }
}

<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">set</span><span class="hljs-params">(V v)</span> </span>{
    <span class="hljs-keyword">if</span> (UNSAFE.compareAndSwapInt(<span class="hljs-keyword">this</span>, stateOffset, NEW, COMPLETING)) {
        outcome = v;
        UNSAFE.putOrderedInt(<span class="hljs-keyword">this</span>, stateOffset, NORMAL); <span class="hljs-comment">// final state</span>
        <span class="hljs-comment">//&#x91CA;&#x653E;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5173;&#x952E;&#x65B9;&#x6CD5;</span>
        finishCompletion();
    }
}

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">finishCompletion</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// assert state &gt; COMPLETING;</span>
    <span class="hljs-keyword">for</span> (WaitNode q; (q = waiters) != <span class="hljs-keyword">null</span>;) {
        <span class="hljs-comment">//cas&#x6E05;&#x7A7A;waiters</span>
        <span class="hljs-keyword">if</span> (UNSAFE.compareAndSwapObject(<span class="hljs-keyword">this</span>, waitersOffset, q, <span class="hljs-keyword">null</span>)) {
            <span class="hljs-comment">//&#x9010;&#x4E2A;&#x6E05;&#x7A7A;&#x94FE;&#x8868;waiters&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;</span>
            <span class="hljs-keyword">for</span> (;;) {
                Thread t = q.thread;
                <span class="hljs-keyword">if</span> (t != <span class="hljs-keyword">null</span>) {
                    q.thread = <span class="hljs-keyword">null</span>;
                    LockSupport.unpark(t);
                }
                WaitNode next = q.next;
                <span class="hljs-comment">//&#x76F4;&#x5230;&#x94FE;&#x8868;&#x4E2D;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x7EBF;&#x7A0B;&#x90FD;&#x5DF2;&#x7ECF;&#x88AB;&#x91CA;&#x653E;</span>
                <span class="hljs-keyword">if</span> (next == <span class="hljs-keyword">null</span>)
                    <span class="hljs-keyword">break</span>;
                q.next = <span class="hljs-keyword">null</span>; <span class="hljs-comment">// unlink to help gc</span>
                q = next;
            }
            <span class="hljs-keyword">break</span>;
        }
    }

    done();

    callable = <span class="hljs-keyword">null</span>;        <span class="hljs-comment">// to reduce footprint</span>
}
</code></pre>
<p>&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#x64CD;&#x4F5C;<code>get()</code></p>
<pre><code class="lang-java">
<span class="hljs-function"><span class="hljs-keyword">public</span> V <span class="hljs-title">get</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException </span>{
    <span class="hljs-keyword">int</span> s = state;
    <span class="hljs-comment">//&#x65B0;&#x5EFA;&#x6216;&#x5904;&#x7406;&#x4E2D;&#x72B6;&#x6001;</span>
    <span class="hljs-keyword">if</span> (s &lt;= COMPLETING)
        s = awaitDone(<span class="hljs-keyword">false</span>, <span class="hljs-number">0L</span>);
    <span class="hljs-keyword">return</span> report(s);
}

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">awaitDone</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> timed, <span class="hljs-keyword">long</span> nanos)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
    <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> deadline = timed ? System.nanoTime() + nanos : <span class="hljs-number">0L</span>;
    WaitNode q = <span class="hljs-keyword">null</span>;
    <span class="hljs-keyword">boolean</span> queued = <span class="hljs-keyword">false</span>;
    <span class="hljs-keyword">for</span> (;;) {
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5DF2;&#x4E2D;&#x65AD;&#x5219;&#x79FB;&#x51FA;waiters</span>
        <span class="hljs-keyword">if</span> (Thread.interrupted()) {
            removeWaiter(q);
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> InterruptedException();
        }
        <span class="hljs-keyword">int</span> s = state;
        <span class="hljs-keyword">if</span> (s &gt; COMPLETING) {
            <span class="hljs-comment">//&#x5B8C;&#x6210;&#x6216;&#x4E2D;&#x65AD;&#x7B49;&#x5176;&#x4ED6;&#x72B6;&#x6001;&#xFF0C;&#x8DF3;&#x51FA;</span>
            <span class="hljs-keyword">if</span> (q != <span class="hljs-keyword">null</span>)
                q.thread = <span class="hljs-keyword">null</span>;
            <span class="hljs-keyword">return</span> s;
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s == COMPLETING) <span class="hljs-comment">// cannot time out yet</span>
            <span class="hljs-comment">//&#x5F53;&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x4E2D;&#x65F6;&#xFF0C;&#x8BA9;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;yield&#xFF0C;&#x8BA9;&#x51FA;cpu</span>
            Thread.yield();
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (q == <span class="hljs-keyword">null</span>)
            q = <span class="hljs-keyword">new</span> WaitNode();
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!queued)
            <span class="hljs-comment">//cas&#x66F4;&#x65B0;waiter&#xFF0C;&#x52A0;&#x5165;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x8282;&#x70B9;</span>
            queued = UNSAFE.compareAndSwapObject(<span class="hljs-keyword">this</span>, waitersOffset,
                                                 q.next = waiters, q);
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (timed) {
            nanos = deadline - System.nanoTime();
            <span class="hljs-keyword">if</span> (nanos &lt;= <span class="hljs-number">0L</span>) {
                removeWaiter(q);
                <span class="hljs-keyword">return</span> state;
            }
            LockSupport.parkNanos(<span class="hljs-keyword">this</span>, nanos);
        }
        <span class="hljs-keyword">else</span>
            LockSupport.park(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<h1 id="completablefuture">CompletableFuture</h1>
<p><code>CompletableFuture</code>&#x76F8;&#x5BF9;&#x4E8E;<code>FutureTask</code>&#x7684;&#x6539;&#x8FDB;&#xFF0C;&#x4E00;&#x53E5;&#x8BDD;&#x603B;&#x7ED3;</p>
<p><code>FutureTask</code>&#x53EA;&#x6709;&#x5355;&#x4E2A;&#x4EFB;&#x52A1;&#x7684;&#x5904;&#x7406;&#x80FD;&#x529B;&#xFF0C;&#x800C;<code>CompletableFuture</code>&#x63D0;&#x4F9B;&#x4E86;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x7EC4;&#x5408;&#x64CD;&#x4F5C;&#x7684;&#x80FD;&#x529B;</p>
<h2 id="api&#x8BF4;&#x660E;">Api&#x8BF4;&#x660E;</h2>
<p>&#x6309;&#x529F;&#x80FD;&#x5206;&#x7C7B;</p>
<ul>
<li>&#x521D;&#x59CB;&#x5165;&#x53E3;&#xFF1A;<code>CompletableFuture.supplyAsync(&#xFF09;</code>&#x3001;<code>runAsync()</code></li>
<li><strong>&#x4EFB;&#x52A1;&#x5904;&#x7406;</strong></li>
<li>&#x4EFB;&#x52A1;&#x7ED3;&#x679C;&#xFF1A;<code>get()</code>&#x3001;<code>join()</code></li>
</ul>
<p>&#x6309;&#x963B;&#x585E;&#x7279;&#x6027;&#x5206;&#x7C7B;</p>
<ul>
<li>&#x540C;&#x6B65;&#xFF1A;&#x4F8B;&#x5982;<code>thenApply()</code></li>
<li>&#x5F02;&#x6B65;&#xFF1A;&#x4F8B;&#x5982;<code>thenApplyAsync()</code></li>
</ul>
<p><strong>&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x7C7B;api</strong>&#x518D;&#x5206;&#x7C7B;</p>
<p>&#x6309;&#x5165;&#x53C2;&#x53CA;&#x8FD4;&#x56DE;&#x503C;&#x5206;&#x7C7B;</p>
<ul>
<li>run&#xFF1A;&#x5355;&#x7EAF;run</li>
<li>accept&#xFF1A;&#x6709;&#x8FD4;&#x56DE;&#x503C;</li>
<li>apply&#xFF1A;&#x63A5;&#x53D7;&#x4E0A;&#x4E2A;future&#x7ED3;&#x679C;&#xFF0C;&#x5E76;&#x751F;&#x6210;&#x65B0;&#x7ED3;&#x679C;</li>
<li>whenComplete&#xFF1A;&#x63A5;&#x6536;&#x4E0A;&#x4E00;&#x7ED3;&#x679C;&#x53CA;&#x5F02;&#x5E38;</li>
</ul>
<p>&#x6309;&#x5904;&#x7406;future&#x6570;&#x91CF;&#x5206;&#x7C7B;</p>
<ul>
<li>&#x5904;&#x7406;&#x5355;&#x4E2A;future&#x7C7B;</li>
<li>&#x591A;&#x4E2A;future&#x805A;&#x5408;&#x5904;&#x7406;</li>
<li>&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x5B8C;&#x6210;&#x540E;&#x5904;&#x7406;</li>
</ul>
<table>
<thead>
<tr>
<th>CFuture Methods</th>
<th>&#x53C2;&#x6570;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>Completion</th>
<th>&#x5206;&#x7C7B;</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>&#x5904;&#x7406;&#x5355;&#x4E2A;future</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>thenRun()</td>
<td>Runnable</td>
<td>&#x5355;&#x7EAF;&#x8FD0;&#x884C;</td>
<td>UniRun extends UniCompletion</td>
<td>run</td>
</tr>
<tr>
<td>thenAccept()</td>
<td>Consumer&lt;? super T&gt;</td>
<td>&#x751F;&#x6210;&#x65B0;&#x7ED3;&#x679C;</td>
<td>UniAccept extends UniCompletion</td>
<td>accept</td>
</tr>
<tr>
<td>thenApply()</td>
<td>Function&lt;? super T,? extends U&gt;</td>
<td>&#x63A5;&#x6536;&#x4E0A;&#x4E00;&#x7ED3;&#x679C;&#xFF0C;&#x5E76;&#x751F;&#x6210;&#x65B0;&#x7ED3;&#x679C;</td>
<td>UniApply extends UniCompletion</td>
<td>apply</td>
</tr>
<tr>
<td>thenCompose()</td>
<td></td>
<td></td>
<td>UniCompose&#x3001;UniRelay extends UniCompletion</td>
<td>compose</td>
</tr>
<tr>
<td>whenComplete()</td>
<td>BiConsumer&lt;? super T, ? super Throwable&gt;</td>
<td>&#x63A5;&#x6536;&#x4E0A;&#x4E00;&#x7ED3;&#x679C;&#x53CA;&#x5F02;&#x5E38;</td>
<td>UniWhenComplete extends UniCompletion</td>
<td>whenComplete</td>
</tr>
<tr>
<td><strong>&#x591A;&#x4E2A;future&#x805A;&#x5408;</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>runAfterBoth()</td>
<td>CompletionStage&lt;?&gt; other,                                            Runnable action</td>
<td>&#x8FD0;&#x884C;&#x4E8E;&#x4E24;&#x4E2A;future&#x4E4B;&#x540E;</td>
<td>BiRun extends BiCompletion</td>
<td>run</td>
</tr>
<tr>
<td>thenAcceptBoth()</td>
<td>CompletionStage&lt;? extends U&gt; other,BiConsumer&lt;? super T, ? super U&gt; action</td>
<td>&#x540C;&#x65F6;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;future&#x7684;&#x7ED3;&#x679C;</td>
<td>BiAccept extends BiCompletion</td>
<td>accept</td>
</tr>
<tr>
<td>thenCombine()</td>
<td>CompletionStage&lt;? extends U&gt; other,BiFunction&lt;? super T,? super U,? extends V&gt; fn</td>
<td>&#x540C;&#x65F6;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;future&#x7ED3;&#x679C;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x65B0;&#x7ED3;&#x679C;</td>
<td>BiApply extends BiCompletion</td>
<td>apply</td>
</tr>
<tr>
<td>allOf()</td>
<td>CompletableFuture&lt;?&gt;... cfs</td>
<td>&#x5168;&#x90E8;future&#x5B8C;&#x6210;&#x540E;</td>
<td>BiRelay extends BiCompletion</td>
<td>replay</td>
</tr>
<tr>
<td><strong>&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x5B8C;&#x6210;</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>runAfterEither()</td>
<td>CompletionStage&lt;?&gt; other,                                              Runnable action</td>
<td>&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x5B8C;&#x6210;&#x540E;</td>
<td>OrRun extends BiCompletion</td>
<td>run</td>
</tr>
<tr>
<td>acceptEither()</td>
<td>CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action</td>
<td>&#x63A5;&#x6536;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x7684;&#x7ED3;&#x679C;</td>
<td>OrAccept extends BiCompletion</td>
<td>accept</td>
</tr>
<tr>
<td>applyToEither()</td>
<td>CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn</td>
<td>&#x63A5;&#x6536;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x7ED3;&#x679C;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x65B0;&#x7ED3;&#x679C;</td>
<td>OrApply extends BiCompletion</td>
<td>apply</td>
</tr>
<tr>
<td>anyOf()</td>
<td>CompletableFuture&lt;?&gt;... cfs</td>
<td>&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x5B8C;&#x6210;&#x540E;</td>
<td>OrRelay extends BiCompletion</td>
<td>replay</td>
</tr>
</tbody>
</table>
<h3 id="runasync&#x4E0E;supplyasync"><code>runAsync()</code>&#x4E0E;<code>supplyAsync()</code></h3>
<ul>
<li>runAsync()&#xFF1A;&#x65E0;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</li>
<li>supplyAsync()&#xFF1A;&#x6709;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</li>
</ul>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">static</span> CompletableFuture&lt;Void&gt; <span class="hljs-title">runAsync</span><span class="hljs-params">(Runnable runnable, Executor executor)</span>

<span class="hljs-keyword">static</span> &lt;U&gt; CompletableFuture&lt;U&gt; <span class="hljs-title">supplyAsync</span><span class="hljs-params">(Supplier&lt;U&gt; supplier, Executor executor)</span>
</span></code></pre>
<p>ps&#xFF1A;&#x6839;&#x636E;&#x89C4;&#x8303;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x6307;&#x5B9A;Executor&#xFF0C;&#x5426;&#x5219;&#x5C06;&#x91C7;&#x7528;&#x9ED8;&#x8BA4;&#x7EBF;&#x7A0B;&#x6C60;<code>ForkJoinPool.commonPool()</code>&#xFF0C;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x8D44;&#x6E90;&#x6D88;&#x8017;&#x95EE;&#x9898;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">static</span> Executor <span class="hljs-title">screenExecutor</span><span class="hljs-params">(Executor e)</span> </span>{
    <span class="hljs-keyword">if</span> (!useCommonPool &amp;&amp; e == ForkJoinPool.commonPool())
        <span class="hljs-keyword">return</span> asyncPool;
    <span class="hljs-keyword">if</span> (e == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NullPointerException();
    <span class="hljs-keyword">return</span> e;
}
</code></pre>
<h3 id="thenaccept&#x4E0E;thenrun"><code>thenAccept()</code>&#x4E0E;<code>thenRun()</code></h3>
<ul>
<li>thenAccept&#xFF1A;&#x63A5;&#x6536;&#x7ED3;&#x679C;&#x5904;&#x7406;</li>
<li>thenRun&#xFF1A;&#x7D27;&#x63A5;&#x4E0A;&#x4E00;&#x6D41;&#x7A0B;&#x5904;&#x7406;</li>
</ul>
<p>&#x5F02;&#x6B65;&#x4E0E;&#x540C;&#x6B65;&#x7684;&#x5DEE;&#x5F02;&#x662F;&#xFF1A;&#x5F02;&#x6B65;&#x7684;&#x5904;&#x7406;&#x662F;&#x5728;&#x4E0A;&#x4E00;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x6240;&#x5728;&#x7EBF;&#x7A0B;&#x57FA;&#x7840;&#x4E0A;&#x518D;&#x6B21;&#x5F02;&#x6B65;&#x5904;&#x7406;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> CompletableFuture&lt;Void&gt; <span class="hljs-title">thenAccept</span><span class="hljs-params">(Consumer&lt;? <span class="hljs-keyword">super</span> T&gt; action)</span> </span>{
    <span class="hljs-keyword">return</span> uniAcceptStage(<span class="hljs-keyword">null</span>, action);
}

<span class="hljs-function"><span class="hljs-keyword">public</span> CompletableFuture&lt;Void&gt; <span class="hljs-title">thenAcceptAsync</span><span class="hljs-params">(Consumer&lt;? <span class="hljs-keyword">super</span> T&gt; action)</span> </span>{
    <span class="hljs-keyword">return</span> uniAcceptStage(asyncPool, action);
}
</code></pre>
<h3 id="anyof&#x4E0E;allof"><code>anyOf()</code>&#x4E0E;<code>allOf()</code></h3>
<ul>
<li><p><code>CompletableFuture&lt;Void&gt; allOf()</code>&#xFF1A;&#x6240;&#x6709;future&#x5B8C;&#x6210;&#x6267;&#x884C;&#xFF0C;&#x4E0D;&#x80FD;&#x4F20;&#x9012;future&#x7ED3;&#x679C;&#x5230;&#x540E;&#x7EED;&#x65B9;&#x6CD5;&#xFF0C;&#x914D;&#x5408;<code>join()</code>&#x4F7F;&#x7528;&#x53EF;&#x4EE5;&#x963B;&#x585E;&#x7B49;&#x5F85;</p>
<ul>
<li><code>CompletableFuture&lt;Object&gt; anyOf()</code>&#xFF1A;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;future&#x5B8C;&#x6210;&#xFF0C;&#x4F20;&#x9012;&#x7B2C;&#x4E00;&#x4E2A;&#x5B8C;&#x6210;future&#x7ED3;&#x679C;&#x5230;&#x540E;&#x7EED;&#x65B9;&#x6CD5;&#xFF0C;&#x914D;&#x5408;<code>get()</code>&#x963B;&#x585E;&#x7B49;&#x5F85;&#x7B2C;&#x4E00;&#x4E2A;&#x7ED3;&#x679C;&#x5B8C;&#x6210;</li>
</ul>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">anyOfallOf</span><span class="hljs-params">()</span>
        <span class="hljs-keyword">throws</span> InterruptedException, ExecutionException </span>{
    CompletableFuture&lt;Integer&gt; future1 = CompletableFuture.supplyAsync(() -&gt; {
        threadSleep(<span class="hljs-number">100</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    });
    CompletableFuture&lt;Integer&gt; future2 = CompletableFuture.supplyAsync(() -&gt; {
        threadSleep(<span class="hljs-number">200</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
    });

    <span class="hljs-comment">//&#x4E0D;&#x963B;&#x585E;</span>
    CompletableFuture.anyOf(future1, future2).thenAccept((r) -&gt; {
        System.out.println(<span class="hljs-string">&quot;result:&quot;</span> + Objects.equals(r, <span class="hljs-number">1</span>));
    });
    System.out.println(<span class="hljs-string">&quot;done&quot;</span>);

    <span class="hljs-comment">//&#x963B;&#x585E;</span>
    CompletableFuture.anyOf(future1, future2).thenAccept((r) -&gt; {
        System.out.println(<span class="hljs-string">&quot;with get() result:&quot;</span> + Objects.equals(r, <span class="hljs-number">1</span>));
    }).get();

    System.out.println(<span class="hljs-string">&quot;with get() done&quot;</span>);
}

<span class="hljs-comment">//output</span>
<span class="hljs-function">done
with <span class="hljs-title">get</span><span class="hljs-params">()</span> result:<span class="hljs-keyword">true</span>
result:<span class="hljs-keyword">true</span>
with <span class="hljs-title">get</span><span class="hljs-params">()</span> done
</span></code></pre>
<h2 id="&#x6E90;&#x7801;">&#x6E90;&#x7801;</h2>
<p>CFutureAPI&#x6309;&#x529F;&#x80FD;&#x5206;&#x7C7B;&#x6709;&#x521D;&#x59CB;&#x5165;&#x53E3;&#x3001;&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x3001;&#x4EFB;&#x52A1;&#x7ED3;&#x679C;3&#x79CD;&#x7C7B;&#x578B;&#xFF1B;&#x800C;&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x8BBE;&#x8BA1;&#x7684;&#x903B;&#x8F91;&#x57FA;&#x672C;&#x662F;&#x4E00;&#x81F4;&#x7684;</p>
<p>&#x4EE5;<code>thenApply()</code>&#x65B9;&#x6CD5;&#x4E3A;&#x4F8B;&#xFF0C;&#x4E0E;&#x5176;&#x4ED6;&#x65B9;&#x6CD5;&#x76F8;&#x6BD4;&#x8F83;&#xFF0C;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#x5728;<strong>&#x7EA2;&#x8272;&#x6B65;&#x9AA4;&#x6846;</strong>&#xFF0C;&#x4EE5;&#x53CA;<code>Completion</code>&#x7684;&#x5B9E;&#x73B0;&#x7C7B;</p>
<p><img src="assets/CompletableFuture.jpg" alt="1580466305537"></p>
<p><img src="assets/1580790576674.png" alt="1580790576674"></p>
<h3 id="&#x6267;&#x884C;thenapply">&#x6267;&#x884C;thenApply()</h3>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CompetableFuture</span></span>{
    <span class="hljs-comment">//API&#x65B9;&#x6CD5;</span>
    <span class="hljs-keyword">public</span> &lt;U&gt; <span class="hljs-function">CompletableFuture&lt;U&gt; <span class="hljs-title">thenApply</span><span class="hljs-params">(
        Function&lt;? <span class="hljs-keyword">super</span> T,? extends U&gt; fn)</span> </span>{
        <span class="hljs-keyword">return</span> uniApplyStage(<span class="hljs-keyword">null</span>, fn);
    }

    <span class="hljs-keyword">private</span> &lt;V&gt; <span class="hljs-function">CompletableFuture&lt;V&gt; <span class="hljs-title">uniApplyStage</span><span class="hljs-params">(
        Executor executor, Function&lt;? <span class="hljs-keyword">super</span> T,? extends V&gt; function)</span> </span>{

        CompletableFuture&lt;V&gt; resultFuture =  <span class="hljs-keyword">new</span> CompletableFuture&lt;V&gt;();

        <span class="hljs-keyword">if</span> (executor != <span class="hljs-keyword">null</span>) {
            <span class="hljs-comment">//executor&#x4E0D;&#x4E3A;&#x7A7A;&#xFF0C;</span>
            UniApply&lt;T,V&gt; completion = <span class="hljs-keyword">new</span> UniApply&lt;T,V&gt;(executor, resultFuture, <span class="hljs-keyword">this</span>, function);
            <span class="hljs-comment">//&#x53C2;&#x8003;&#x4EE5;&#x4E0B;push()&#x65B9;&#x6CD5;&#x5206;&#x6790;</span>
            push(completion);
            completion.tryFire(SYNC);
        }<span class="hljs-keyword">else</span>{
            <span class="hljs-comment">//executor&#x4E3A;&#x7A7A;&#xFF0C;&#x76F4;&#x63A5;&#x540C;&#x6B65;&#x6267;&#x884C;resultFuture.uniApply()</span>
            resultFuture.uniApply(<span class="hljs-keyword">this</span>, function, <span class="hljs-keyword">null</span>)
        }
        <span class="hljs-keyword">return</span> resultFuture;
    }
}

<span class="hljs-comment">//Completion&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x7C7B;</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UniApply</span>&lt;<span class="hljs-title">T</span>,<span class="hljs-title">V</span>&gt; <span class="hljs-keyword">extends</span> <span class="hljs-title">UniCompletion</span>&lt;<span class="hljs-title">T</span>,<span class="hljs-title">V</span>&gt; </span>{
    Function&lt;? <span class="hljs-keyword">super</span> T,? extends V&gt; fn;
    UniApply(Executor executor, CompletableFuture&lt;V&gt; dep,
             CompletableFuture&lt;T&gt; src,
             Function&lt;? <span class="hljs-keyword">super</span> T,? extends V&gt; fn) {
        <span class="hljs-comment">//super()&#xFF0C;&#x8BBE;&#x7F6E;dep&#x3001;src&#x3001;executor</span>
        <span class="hljs-keyword">super</span>(executor, dep, src); <span class="hljs-keyword">this</span>.fn = fn;
    }
    <span class="hljs-function"><span class="hljs-keyword">final</span> CompletableFuture&lt;V&gt; <span class="hljs-title">tryFire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> mode)</span> </span>{
        CompletableFuture&lt;V&gt; d; CompletableFuture&lt;T&gt; a;
        <span class="hljs-comment">//dep = d = &#x8FD4;&#x56DE;future</span>
        <span class="hljs-comment">//src = a = &#x539F;future</span>
        <span class="hljs-comment">//fn &#x5B9E;&#x9645;&#x4EFB;&#x52A1;</span>
        <span class="hljs-comment">//mode : SYNC: 0</span>
        <span class="hljs-keyword">if</span> ((d = dep) == <span class="hljs-keyword">null</span> ||
            !d.uniApply(a = src, fn, mode &gt; <span class="hljs-number">0</span> ? <span class="hljs-keyword">null</span> : <span class="hljs-keyword">this</span>))
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        dep = <span class="hljs-keyword">null</span>; src = <span class="hljs-keyword">null</span>; fn = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">return</span> d.postFire(a, mode);
    }
}
</code></pre>
<h3 id="&#x52A0;&#x5165;&#x961F;&#x5217;">&#x52A0;&#x5165;&#x961F;&#x5217;</h3>
<pre><code class="lang-java"><span class="hljs-comment">/** Pushes the given completion (if it exists) unless done. */</span>
<span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">push</span><span class="hljs-params">(UniCompletion&lt;?,?&gt; c)</span> </span>{
    <span class="hljs-keyword">if</span> (c != <span class="hljs-keyword">null</span>) {
        <span class="hljs-keyword">while</span> (result == <span class="hljs-keyword">null</span> &amp;&amp; !tryPushStack(c))
            lazySetNext(c, <span class="hljs-keyword">null</span>); <span class="hljs-comment">// clear on failure</span>
    }
}

<span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryPushStack</span><span class="hljs-params">(Completion c)</span> </span>{
    <span class="hljs-comment">//stack: &#x5F53;&#x524D;future&#x7684;Completion&#x961F;&#x5217;&#xFF0C;FIFO</span>
    Completion h = stack;
    <span class="hljs-comment">//&#x52A0;&#x5165;&#x5230;stack&#x961F;&#x5217;&#x5C3E;</span>
    lazySetNext(c, h);
    <span class="hljs-comment">//CAS&#x8BBE;&#x4E3A;&#x65B0;&#x961F;&#x5217;</span>
    <span class="hljs-keyword">return</span> UNSAFE.compareAndSwapObject(<span class="hljs-keyword">this</span>, STACK, h, c);
}

<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lazySetNext</span><span class="hljs-params">(Completion c, Completion next)</span> </span>{
    <span class="hljs-comment">//&#x5F53;&#x524D;future&#x7684;&#x4E0B;&#x4E00;&#x4E2A;Completion</span>
    UNSAFE.putOrderedObject(c,  NEXT, next);
}
</code></pre>
<h1 id="forkjointask">ForkJoinTask</h1>
<p>TODO</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; tgor.top 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-04-23 15:32:53
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Java之Lock.html#reentrantreadwritelock" class="navigation navigation-prev " aria-label="Previous page: Java Lock之ReentrantReadWriteLock">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Java之Future.html#futuretask" class="navigation navigation-next " aria-label="Next page: Java Future之FutureTask">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java Future","level":"1.4.6","depth":2,"next":{"title":"Java Future之FutureTask","level":"1.4.7","depth":2,"anchor":"#futuretask","path":"Java-Base/Java之Future.md","ref":"Java-Base/Java之Future.md#futuretask","articles":[]},"previous":{"title":"Java Lock之ReentrantReadWriteLock","level":"1.4.5","depth":2,"anchor":"#reentrantreadwritelock","path":"Java-Base/Java之Lock.md","ref":"Java-Base/Java之Lock.md#reentrantreadwritelock","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","splitter","tbfed-pagefooter","page-toc-button","-search","search-pro","popup","back-to-top-button","code","copy-code-button"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy tgor.top 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"fontsettings":{"theme":"sepia","family":"serif","size":1},"highlight":{},"page-toc-button":{"maxTocDepth":2,"minTocSize":2},"back-to-top-button":{},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tgor","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"tgor note","gitbook":"*","description":"tgor生活工作记录"},"file":{"path":"Java-Base/Java之Future.md","mtime":"2020-04-23T07:32:53.268Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-29T08:43:24.127Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

